from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from apps.goods.models import SpecificationOption, SPUSpecification
from apps.meiduo_admin.serializers.options import SpecificationOptionSerializer, SPUSpecificationSerializer

from apps.meiduo_admin.utils import UserPageNum


class SpecificationOptionGoodsView(ModelViewSet):
    '''
        规格选项表的增删改查
    '''
    # 指定查询集
    queryset = SpecificationOption.objects.all()
    # 指定序列化器
    serializer_class = SpecificationOptionSerializer
    # 指定分页器
    pagination_class = UserPageNum
    # 指定权限
    permission_classes = [IsAdminUser]

    # 自定义方法获取规格表数据
    def simple(self, request):
        # 1、查询spu规格表获取所有规格数据
        option = SPUSpecification.objects.all()
        # 2、序列化返回规格数据
        s = SPUSpecificationSerializer(option, many=True)
        # 拼接返回规格
        for data in s.data:
            data['name'] = data['spu'] + ':' + data['name']
        return Response(s.data)
